function [ numMatch, Iout ] = Characterize( Iin, IS_DEBUG )
%CHARACTERIZE Summary of this function goes here
%   Detailed explanation goes here
numMark=1;
markArr{numMark} = [1 1 1 1 1;
    1 0 0 0 0;
    1 0 0 0 0;
    1 0 0 0 0;
    1 0 0 0 0];
numMark = numMark+1; %2
markArr{numMark} = [1 1 1 1 1;
    0 0 0 0 1;
    0 0 0 0 1;
    0 0 0 0 1;
    0 0 0 0 1];
numMark = numMark+1; %3
markArr{numMark} = [1 0 0 0 0;
    1 0 0 0 0;
    1 0 0 0 0;
    1 0 0 0 0;
    1 1 1 1 1];
numMark = numMark+1; %4
markArr{numMark} = [0 0 0 0 1;
    0 0 0 0 1;
    0 0 0 0 1;
    0 0 0 0 1;
    1 1 1 1 1];
numMark = numMark+1; %5
markArr{numMark} = [1 1 1 1 1;
					1 1 1 1 1;
					1 0 1 1 1;
					1 0 0 1 1;
					1 0 0 0 1];
numMark = numMark+1; %6
markArr{numMark} = [1 0 0 0 1;
    1 0 0 1 1;
    1 0 1 1 1;
    1 1 1 1 1;
    1 1 1 1 1];
numMark = numMark+1; %7
markArr{numMark} = [1 1 1 1 1;
    1 1 0 0 0;
    1 1 1 0 0;
    1 1 1 1 0;
    1 1 1 1 1];
numMark = numMark+1; %8
markArr{numMark} = [1 1 1 1 1;
    0 0 0 1 1;
    0 0 1 1 1;
    0 1 1 1 1;
    1 1 1 1 1];
numMark = numMark+1; %9
markArr{numMark} = [1 0 0 0 1;
    1 0 0 1 1;
    1 0 1 1 1;
    1 1 1 1 1;
    1 1 1 1 1];
numMark = numMark+1; %10
markArr{numMark} = [1 1 1 1 1;
                    1 1 1 1 1;
                    1 1 1 0 1;
                    1 1 0 0 1;
                    1 0 0 0 1];
numMark = numMark+1; %11
markArr{numMark} = [1 1 1 1 1;
    0 1 1 1 1;
    0 0 1 1 1;
    0 0 0 1 1;
    1 1 1 1 1];
numMark = numMark+1; %12
markArr{numMark} = [1 1 1 1 1;
    1 1 0 0 0;
    1 1 1 0 0;
    1 1 1 1 0;
    1 1 1 1 1];
numMark = numMark+1; %13
markArr{numMark} =     [ 1 0 0 0 0 0 1 ;
                         1 1 0 0 0 1 1 ;
                         1 1 0 0 0 1 1 ;
                         1 1 1 0 1 1 1 ;
                         1 1 1 1 1 1 1 ];

                     numForceMark=1;
markForceOne{numForceMark}=[1 1 1 0 0;
    1 0 0 0 0;
    1 0 0 0 0;
    0 0 0 0 0;
    0 0 0 0 0];
numForceMark = numForceMark+1; %2
markForceOne{numForceMark}=[0 0 1 1 1;
    0 0 0 0 1;
    0 0 0 0 1;
    0 0 0 0 0;
    0 0 0 0 0];
numForceMark = numForceMark+1; %3
markForceOne{numForceMark}=[0 0 0 0 0;
    0 0 0 0 0;
    1 0 0 0 0;
    1 0 0 0 0;
    1 1 1 0 0];
numForceMark = numForceMark+1; %4
markForceOne{numForceMark}=[0 0 0 0 0;
    0 0 0 0 0;
    0 0 0 0 1;
    0 0 0 0 1;
    0 0 1 1 1];
numForceMark = numForceMark+1; %5
markForceOne{numForceMark}=[1 1 0 0 0;
                            1 0 1 0 0;
                            1 0 0 1 0;
                            0 0 0 0 1;
                            0 0 0 0 0];
numForceMark = numForceMark+1; %6
markForceOne{numForceMark}=[0 0 0 0 0;
                            0 0 0 0 1;
                            1 0 0 1 0;
                            1 0 1 0 0;
                            1 1 0 0 0];
numForceMark = numForceMark+1; %7
markForceOne{numForceMark}=[1 1 1 0 0;
                            1 0 0 0 0;
                            0 1 0 0 0;
                            0 0 1 0 0;
                            0 0 0 1 0];
numForceMark = numForceMark+1; %8
markForceOne{numForceMark}=[0 0 1 1 1;
                            0 0 0 0 1;
                            0 0 0 1 0;
                            0 0 1 0 0;
                            0 1 0 0 0];
numForceMark = numForceMark+1; %9
markForceOne{numForceMark}=[0 0 0 0 0;
                            0 0 0 0 1;
                            1 0 0 1 0;
                            1 0 1 0 0;
                            1 1 0 0 0];
numForceMark = numForceMark+1; %10
markForceOne{numForceMark}=[0 0 0 1 1;
                            0 0 1 0 1;
                            0 1 0 0 1;
                            1 0 0 0 0;
                            0 0 0 0 0];
numForceMark = numForceMark+1; %11
markForceOne{numForceMark}=[0 1 0 0 0;
                            0 0 1 0 0;
                            0 0 0 1 0;
                            0 0 0 0 1;
                            0 0 1 1 1];
numForceMark = numForceMark+1; %12
markForceOne{numForceMark}=[1 1 1 0 0;
                            1 0 0 0 0;
                            0 1 0 0 0;
                            0 0 1 0 0;
                            0 0 0 1 0];
numForceMark = numForceMark+1; %13
markForceOne{numForceMark} =     [ 0 0 0 0 0 0 0 ;
                         0 0 0 0 0 0 0 ;
                         0 1 0 0 0 1 0 ;
                         0 0 1 0 1 0 0 ;
                         0 0 1 1 1 0 0 ];



Irgb = repmat(Iin,[1 1 3]);
Irgb = cat(3,Iin,Iin,Iin);
[X Y] = size(Iin);
Iin=uint8(Iin);
tUp =11;
tDown = 8;
numMatch = 0;
for i=1:X
    for j=1:Y
        isMatch = 0;
        for imark = 1:numMark
            if isMatch
                break;
            end
            if imark <5
                tUp = 7;
                tDown = 2;
            elseif imark <13
                tUp = 17;
                tDown = 2;
            else
                tUp = 21;
                tDown = 5;
            end
            mark = uint8(markArr{imark});
            forceMark = uint8(markForceOne{imark});
            [X1 Y1] = size(mark);
            if (i<X-X1 && j<Y-Y1)
                output = andMatrix(Iin, mark, forceMark, i, j, tUp, tDown);
                if(output>0)
                    isMatch=1;
                    numMatch = numMatch+1;
                    for m = 1:X1
                        for n = 1:Y1
                            if mark(m,n)
                                Irgb(i+m-1,j+n-1,:)=[1 0 0];
                            end
                        end
                    end
                end
            end
        end
    end

end
Iout = Irgb;

end

function [output] = andMatrix(Iin, mark, forceMark, x, y, tUp, tDown)

[X2 Y2] = size(mark);

Isub = Iin(x : x+X2-1, y: y+Y2-1);


sumOne = sum(sum(forceMark));
if sumOne == sum(sum(Isub.*forceMark))
    output = 1;
    Iand = Isub.*mark;
    sumI1= sum(sum(Iand));
    markInvert = 1 - mark;
    Iand = Isub.*markInvert;
    sumI2= sum(sum(Iand));
    if( sumI1>tUp && sumI2 <tDown)
        output=1;
    else
        output=0;
    end
else
    output = 0;
end

end

